package com.tz.liveplayermodule;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pili.pldroid.player.IMediaController;
import com.pili.pldroid.player.PLOnInfoListener;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.tz.liveplayermodule.util.CommonUtils;
import com.tz.liveplayermodule.util.MediaController;

import java.io.File;

import static com.tz.liveplayermodule.util.PlayConfigView.BACKSTAGE_PLAY_TAG;
import static com.tz.liveplayermodule.util.Utils.createAVOptions;

public class FullScreenPlayVideoActivity extends Activity {

    PLVideoTextureView videoView;
    String videoPath;
    String coverPath;
    ImageButton stopPlayImage;
    ImageView coverImage;
    TextView detailText;
    TextView nameText;
    View loadingView;
    ImageButton fullScreenImage;
    private PowerManager.WakeLock wakeLock;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        CommonUtils.keepScreenOn(wakeLock, this, true);

        setContentView(R.layout.activity_full_screen_video);

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        coverImage = findViewById(R.id.cover_image);
        stopPlayImage = findViewById(R.id.cover_stop_play);
        videoView = findViewById(R.id.video_texture_view);
        loadingView = findViewById(R.id.loading_view);
        fullScreenImage = findViewById(R.id.full_screen_image);

        final MediaController mediaController = findViewById(R.id.media_controller);

        videoView.setAVOptions(createAVOptions());
        videoView.setBufferingIndicator(loadingView);
        videoView.setMediaController(mediaController);
        videoView.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);
        videoView.setLooping(true);
        videoView.setOnInfoListener(new PLOnInfoListener() {
            @Override
            public void onInfo(int i, int i1) {
                if (i == PLOnInfoListener.MEDIA_INFO_VIDEO_RENDERING_START) {
                    coverImage.setVisibility(View.GONE);
                    stopPlayImage.setVisibility(View.GONE);
                    mediaController.hide();
                }
            }
        });

        IMediaController.MediaPlayerControl player = mediaController.getPlayer();

        coverImage.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onClick(View view) {
                stopCurVideoView();
                startCurVideoView();
            }
        });

        ImageView change_size_img = (ImageView) findViewById(R.id.change_size_img);
        final LinearLayout change_size_layout = (LinearLayout) findViewById(R.id.change_size_layout);
        ImageView change_size_img1 = (ImageView) findViewById(R.id.change_size_img1);
        ImageView change_size_img2 = (ImageView) findViewById(R.id.change_size_img2);
        ImageView change_size_img3 = (ImageView) findViewById(R.id.change_size_img3);
        ImageView change_size_img4 = (ImageView) findViewById(R.id.change_size_img4);
        change_size_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.VISIBLE);
            }
        });
        change_size_img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                videoView.setPlaySpeed(1.0f);
            }
        });
        change_size_img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                videoView.setPlaySpeed(1.25f);
            }
        });
        change_size_img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                videoView.setPlaySpeed(1.5f);
            }
        });
        change_size_img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                videoView.setPlaySpeed(2.0f);
            }
        });


        videoPath = getIntent().getStringExtra("filePath");
        if (getIntent().getFlags() == 10002) {
            System.out.println("Current=2=:" + getIntent().getLongExtra("Current", 0));

            videoView.setVideoPath(videoPath);
            videoView.start();
            loadingView.setVisibility(View.VISIBLE);
            stopPlayImage.setVisibility(View.GONE);

//            player.seekTo(getIntent().getLongExtra("Current", 0));
//            videoView.seekTo(getIntent().getLongExtra("Current", 0));
        } else {
            startCurVideoView();
        }
    }

    public void startCurVideoView() {
        if (videoView != null) {
            File videoFile = new File(videoPath);
            Uri videoUri = Uri.fromFile(videoFile);

            videoView.setVideoURI(videoUri);
            videoView.start();
            loadingView.setVisibility(View.VISIBLE);
            stopPlayImage.setVisibility(View.GONE);
        }
    }

    public void restartCurVideoView() {
        if (videoView != null) {
            videoView.start();
            stopPlayImage.setVisibility(View.GONE);
        }
    }

    public boolean isCurVideoPlaying() {
        return videoView != null && videoView.isPlaying();
    }

    public boolean needBackstagePlay() {
        return videoView != null && BACKSTAGE_PLAY_TAG.equals(videoView.getTag());
    }


    public void pauseCurVideoView() {
        if (videoView != null) {
            videoView.pause();
            loadingView.setVisibility(View.GONE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void resetConfig() {
        if (videoView != null) {
            videoView.setRotation(0);
            videoView.setMirror(false);
            videoView.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public void stopCurVideoView() {
        if (videoView != null) {
            resetConfig();
            videoView.stopPlayback();
            loadingView.setVisibility(View.GONE);
            coverImage.setVisibility(View.VISIBLE);
            stopPlayImage.setVisibility(View.VISIBLE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopCurVideoView();
    }
}
